home *** CD-ROM | disk | FTP | other *** search
/ The Business Master (3rd Edition) / The Business Master (3rd Edition).iso / files / business / stata3 / statkit.tut < prev    next >
Encoding:
Text File  |  1988-08-30  |  12.7 KB  |  401 lines

  1. set output error
  2. set display page 23
  3. set more 1
  4. #delimit ;
  5.  
  6. di _n(13) in wh
  7. "  ___  ____  ____  ____  ____ tm" _n
  8. " /__    /   ____/   /   ____/" _n
  9. "___/   /   /___/   /   /___/    Stat.Kit:  Programmed Extensions to Stata" _n
  10. "-------------------------------------------------------------------------"
  11. _n(2) ;
  12.  
  13. di in gr
  14. "Stat.Kit is a collection of programs written in Stata that provides a variety"
  15. _n
  16. "of statistical tests.  Although Stat.Kit is written in Stata's programming"
  17. _n
  18. "language, you do not have to know anything about the programming language to"
  19. _n
  20. "use it.  We begin by loading the kit:" _n(2) ;
  21.  
  22.  
  23. #delimit cr
  24. mac def path
  25. capture run nullfile.tut
  26. if _rc {
  27.     mac def path "\stata\"
  28.     capture run %path`nullfile.tut
  29.     if _rc {
  30.         mac def path "/usr/stata/"
  31.         capture run %path`nullfile.tut
  32.         if _rc {
  33.             #delimit ;
  34.             di in red
  35. "I cannot find the other tutorial files.  I have looked in the current" _n
  36. "directory and in \stata (DOS) or /usr/stata (Unix).  Is Stata installed" _n
  37. "correctly?" _n(2)
  38. "In any case, I cannot run the tutorial." ;
  39.             #delimit cr
  40.             exit
  41.         }
  42.     }
  43. }
  44. macro define F5 "do %path`contents.tut;"
  45. macro define F6 "do %path`statkit.tut;"
  46.  
  47. drop _all
  48. label drop _all
  49.  
  50. #delimit ;
  51. di in wh ". run %path`Stat.Kit" ;
  52. run %path`Stat.Kit ;
  53. di ; set more 0 ; more ; set more 1 ;
  54.  
  55. di _n(2) in wh
  56. "Overview" _n
  57. "--------" _n ;
  58.  
  59. di in gr
  60. "Stat.Kit provides fifteen new commands:" _n(2)
  61. _col(8) in wh "dbeta" in gr _col(20)
  62. "calculate DF-Betas influence statistics"  _n
  63. _col(8) in wh "genrank" in gr _col(20)
  64. "create ranks of variable accounting for ties" _n
  65. _col(8) in wh "genstd" in gr _col(20)
  66. "create standardized (mean 0, variance 1) variable" _n
  67. _col(8) in wh "glogit" in gr _col(20) "grouped logit" _n
  68. _col(8) in wh "gprobit" in gr _col(20) "grouped probit" _n
  69. _col(8) in wh "ksmirnov" in gr _col(20)
  70. "Kolmogorov-Smirnov equality-of-distributions test" ;
  71.  
  72. di in gr
  73. _col(8) in wh "kwallis" in gr _col(20)
  74. "Kruskal-Wallis one-way analysis-of-variance test" _n
  75. _col(8) in wh "means" in gr _col(20)
  76. "arithmetic, geometric, and harmonic means" _n
  77. _col(8) in wh "ranksum" in gr _col(20)
  78. "Wilcoxon rank-sum (Mann-Whitney two sample) statistic" _n
  79. _col(8) in wh "regdw" in gr _col(20)
  80. in wh "regress" in gr " with Durbin-Watson statistic" _n
  81. _col(8) in wh "signrank" in gr _col(20)
  82. "Wilcoxon matched-pairs signed-ranks test" _n
  83. _col(8) in wh "signtest" in gr _col(20)
  84. "equality of medians for matched observations test" _n
  85. _col(8) in wh "spearman" in gr _col(20)
  86. "Spearman rank correlation coefficient" _n
  87. _col(8) in wh "teststd" in gr _col(20)
  88. "test equality of variances and against known constant" _n
  89. _col(8) in wh "ttest" in gr _col(20)
  90. "t-tests of all sorts" _n ;
  91.  
  92. di in gr
  93. "We will not be able to demonstrate them all here.  At the conclusion of this"
  94. _n
  95. "tutorial, you might type '"
  96. in wh "help Stat.Kit" in gr "' to learn more about the commands." ;
  97. set more 0 ; more ; set more 1  ;
  98.  
  99.  
  100. di _n(2) in wh
  101. "Stata Demonstration Version Note" _n
  102. "--------------------------------" _n ;
  103.  
  104. di in gr
  105. "We were forced to omit the "
  106. in wh "ranksum" in gr " command from the demonstration version of" _n
  107. "Stat.Kit.  During the process of making its calculations, "
  108. in wh "ranksum" in gr " writes a" _n
  109. "temporary data set, something the demonstration version of Stata cannot do."
  110. _n
  111. "If you attempt to use "
  112. in wh "ranksum" in gr ", you will get the message '" in ye "not available in" _n
  113. "demonstration version" in gr "'."  _n(2)
  114. "If you are using the Unix version of Stata in demonstration mode, do not use"
  115. _n
  116. "the "
  117. in wh "ranksum" in gr
  118. " command.  Your version of Stat.Kit contains the real " in wh "ranksum" _n
  119. in gr
  120. "program, but it will not work in demonstration mode." _n(12) ;
  121. set more 0 ; more ; set more 1 ;
  122.  
  123. di _n(2) in wh
  124. "Using Stat.Kit" _n
  125. "--------------" _n ;
  126.  
  127. di in gr
  128. "Let's try some of the Stat.Kit commands.  Before we can try them, however, we"
  129. _n
  130. "need a statistical problem, so consider the following:" _n(2)
  131. "You are testing the effectiveness of a new fuel additive.  You run an exper-"
  132. _n
  133. "iment with 12 cars.  You first run each without the fuel treatment and measure"
  134. _n
  135. "the mileage.  You then add the treatment and repeat the experiment.  The "
  136. "result" _n
  137. "of the experiment is:" _n ;
  138.  
  139. di in gr
  140. _col(12)
  141. _col(12) "Without      With" _col(43) "Without      With" _n
  142. _col(11) "Treatment   Treatment" _col(42) "Treatment   Treatment" _n
  143. _col(10) _dup(33) "-" _col(41) _dup(33) "-" _n  in ye
  144. _col(15) "20          24" _col(46) "18          17" _n
  145. _col(15) "23          25" _col(46) "24          28" _n
  146. _col(15) "21          21" _col(46) "20          24" _n
  147. _col(15) "25          22" _col(46) "24          27" _n
  148. _col(15) "18          23" _col(46) "23          21" _n
  149. _col(15) "17          18" _col(46) "19          23" _n  ;
  150.  
  151. di in gr
  152. "Stata is now loading the data." _n ;
  153.  
  154. input mpg0 mpg1 ;
  155. 20  24;
  156. 23  25;
  157. 21  21;
  158. 25  22;
  159. 18  23;
  160. 17  18;
  161. 18  17;
  162. 24  28;
  163. 20  24;
  164. 24  27;
  165. 23  21;
  166. 19  23;
  167. end;
  168. set more 0 ; more ; set more 1 ;
  169.  
  170. di _n(9) in wh _dup(79) "-" _n in gr
  171. "Just because we have loaded Stat.Kit does not mean we cannot use all the" _n
  172. "other Stata commands, so let's first "
  173. in wh "summarize" in gr " the data.  We have named the" _n
  174. "variables mpg0 and mpg1, mileage without and with the fuel treatment, respec-"
  175. _n
  176. "tively." _n
  177. in wh _dup(79) "-" _n(3)
  178. ". summarize" ;
  179. noisily summarize ;
  180. di ; set more 0 ; more ; set more 1 ;
  181.  
  182. di _n(2) in wh _dup(79) "-" _n in gr
  183. "Stat.Kit's "
  184. in wh "means" in gr
  185. " command will tell us the arithmetic, geometric, and harmonic"
  186. _n
  187. "means:" _n
  188. in wh _dup(79) "-" _n(2)
  189. ". means" ;
  190. means ;
  191. di _n ; set more 0 ; more ; set more 1 ;
  192.  
  193.  
  194. di _n(2) in wh _dup(79) "-" _n in gr
  195. "Let's test whether the arithmetic mean of mileage without the fuel treatment"
  196. _n
  197. "is 20 mpg:" _n
  198. in wh _dup(79) "-" _n(4)
  199. ". ttest mpg0=20" ;
  200. ttest mpg0=20 ;
  201.  
  202. di _n(4) in wh _dup(79) "-" _n in gr
  203. "We cannot reject the hypothesis that the mean is 20, at least at any signifi-"
  204. _n
  205. "cance level below 23%." _n
  206. in wh _dup(79) "-" _n(2) ;
  207. set more 0 ; more ; set more 1 ;
  208.  
  209. di _n(2) in wh _dup(79) "-" _n in gr
  210. "Let's test whether the mean mileages with and without the treatment are equal."
  211. _n
  212. "Since the same cars are used to test the effectiveness of the treatment, we" _n
  213. "use a paired t-test:" _n
  214. in wh _dup(79) "-" _n(2)
  215. ". ttest mpg0=mpg1, paired" ;
  216. ttest mpg0=mpg1, paired ;
  217.  
  218. di _n(2) in wh _dup(79) "-" _n in gr
  219. "The means are different at the 5% level." _n
  220. in wh _dup(79) "-" _n ;
  221. set more 0 ; more ; set more 1 ;
  222.  
  223. di _n(2) in wh _dup(79) "-" _n in gr
  224. "If different cars had been used to test the effectiveness of the fuel treat-"
  225. _n
  226. "ment, the data would not be paired.  In that case, we would use the (standard)"
  227. _n
  228. "unpaired t-test:" _n
  229. in wh _dup(79) "-" _n(2)
  230. ". ttest mpg0=mpg1" ;
  231. ttest mpg0=mpg1 ;
  232.  
  233. di _n(2) in wh _dup(79) "-" _n in gr
  234. "We see that the means are not different at the 5% level in this case." _n
  235. in wh _dup(79) "-" _n ;
  236. set more 0 ; more ; set more 1 ;
  237.  
  238. di _n(2) in wh _dup(79) "-" _n in gr
  239. "Let's summarize.   We obtained the following two results:" _n(2)
  240. _col(18) "method" _col(33) "t-statistic"  _col(49) "significance level"_n
  241. _col(17) "--------" _col(33) "----------" _col(49) _dup(17) "-" _n
  242. _col(18) "paired" _col(35) in ye "-2.24" _col(55) "0.0463" in gr _n
  243. _col(17) "unpaired" _col(35) in ye "-1.43" _col(55) "0.1667" _n ;
  244.  
  245. di in gr
  246. "Only one of these results can be right.  If the same cars were used with and"
  247. _n
  248. "without the fuel treatment, then you use the paired t-test.  The question is"
  249. _n
  250. "not whether two independent samples share the same mean, but whether the same"
  251. _n
  252. "car yielded improved mileage when the fuel treatment was added." _n(2)
  253. "If 24 different cars were used, 12 without and another 12 with the fuel treat-"
  254. _n
  255. "ment, then you use the unpaired result.  In this case you do want to test" _n
  256. "whether two independent samples share the same mean." _n(2)
  257. "The data is really for 12 cars, before-and-after.  We just wanted to show you"
  258.  _n
  259. "we could do it either way." _n(2)
  260. "If we typed '"
  261. in wh "ttest mpg0=mpg1, unequal" in gr
  262. "', Stata would perform the test for un-" _n
  263. "paired data without assuming that the variances are equal." _n
  264. in wh _dup(79) "-" _n ;
  265. set more 0 ; more ; set more 1 ;
  266.  
  267.  
  268. di _n(14) in wh _dup(79) "-" _n in gr
  269. "Next, we'll test whether the medians are the same using the "
  270. in wh "signtest" in gr " command." _n
  271. in wh "signtest" in gr
  272. " tests the equality of medians for matched pairs of observations.  It"
  273. _n
  274. "does this by calculating the difference between the two variables.  The hypo-"
  275. _n
  276. "thesis that the medians are equal is equivalent to the hypothesis that the" _n
  277. "median of the differences is zero.  This, in turn, is equivalent to the hypo-"
  278. _n
  279. "thesis that the true proportion of positive (negative) signs is one-half."
  280. _n
  281. in wh _dup(79) "-" _n(4)
  282. ". signtest mpg0=mpg1" ;
  283. set more 0 ; more ; set more 1 ;
  284. signtest mpg0=mpg1 ;
  285. di _n in wh _dup(79) "-" _n in gr
  286. "(There is one exact equality in the data, which "
  287. in wh "signtest" in gr " allocates equally to"
  288. _n
  289. "positive and negative.)" _n
  290. in wh _dup(79) "-" ;
  291. set more 0 ; more ; set more 1 ;
  292.  
  293. di _n(2) in wh _dup(79) "-" _n in gr
  294. "Let's test whether the distributions are the same using the Wilcoxon matched-"
  295. _n
  296. "pairs signed-ranks test:" _n
  297. in wh _dup(79) "-" _n(3)
  298. ". signrank mpg0=mpg1" ;
  299. signrank mpg0=mpg1 ;
  300.  
  301. di _n(3) in wh _dup(79) "-" _n in gr
  302. "We find that the distributions are different at the 5% level." _n
  303. in wh _dup(79) "-" _n ;
  304. set more 0 ; more ; set more 1 ;
  305.  
  306. di _n(2) in wh _dup(79) "-" _n in gr
  307. "We now switch data sets.  We are going to use state data from the 1980 Census"
  308. _n
  309. "which we have previously stored in the file called census.dta.  We will only"
  310. _n
  311. "need a few variables from this data.  We "
  312. in wh "describe" in gr " the data below and on the" _n
  313. "next screen we will " in wh "list" in gr " some of it for you." _n
  314. in wh _dup(79) "-" _n(2)
  315. ". use %path`census, clear" ;
  316. noisily use %path`census, clear ;
  317. di _n in wh ". keep state marriage medage pop18p" ;
  318. keep state marriage medage pop18p ;
  319. di _n in wh ". describe" ;
  320. noisily describe ;
  321. set more 0 ; more ; set more 1 ;
  322. di _n in wh ". list in 1/20" ;
  323. noisily list in 1/20 ;
  324. set more 0 ; more ; set more 1 ;
  325.  
  326.  
  327. di _n(2) in wh _dup(79) "-" _n in gr
  328. "We want to examine the marriage rate, which we define as the number of mar-" _n
  329. "riages divided by the state's population aged 18 years and over.  First, we" _n
  330. in wh "generate"
  331. in gr " the variable and " in wh "summarize" in gr " it:" _n
  332. in wh _dup(79) "-" _n(2)
  333. ". generate mrgrate = marriage/pop18p" _n(2)
  334. ". summarize mrgrate" ;
  335. gen mrgrate=marriage/pop18p ;
  336. noisily summarize mrgrate ;
  337.  
  338. di _n(2) in wh _dup(79) "-" _n in gr
  339. "We now examine the correlation of mrgrate with age.  Stata's built-in "
  340. in wh "corre" in gr "-"
  341. _n in wh "late" in gr
  342. " command calculates the Pearson product-moment correlation coefficient." _n
  343. "Stat.Kit's "
  344. in wh "spearman" in gr
  345. " command calculates the Spearman rank correlation coef-" _n
  346. "ficient:" _n
  347. in wh _dup(79) "-" _n(2)
  348. ". correlate mrgrate medage" ;
  349. set more 0 ; more ; set more 1 ;
  350. noisily correlate mrgrate medage ;
  351. di _n in wh ". spearman mrgrate medage" ;
  352. noisily spearman mrgrate medage ;
  353. set more 0 ; more ; set more 1 ;
  354.  
  355. di _n(2) in wh _dup(79) "-" _n in gr
  356. "We find that the standard (Pearson) correlation coefficient between mrgrate"
  357. _n
  358. "and medage is "
  359. in ye "-0.0177" in gr ", whereas the Spearman rank correlation coefficient is"
  360. _n
  361. in ye "-0.5551." in gr "  Let's find out why:" _n
  362. in wh _dup(79) "-" _n(2)
  363. ". summarize mrgrate, detail" ;
  364. noisily summarize mrgrate, detail ;
  365. set more 0 ; more ; set more 1 ;
  366.  
  367. di _n in wh _dup(79) "-" _n in gr
  368. "One state has a marriage rate of "
  369. in ye "0.1955" in gr ", whereas the second highest rate in" _n
  370. "our data is " in ye "0.0247" in gr "!" _n
  371. in wh _dup(79) "-" _n ;
  372. set more 0 ; more ; set more 1 ;
  373.  
  374. di _n in wh ". list if mrgrate>.0247" ;
  375. noisily list if mrgrate>.0247 ;
  376. di _n(2) in wh _dup(79) "-" _n in gr
  377. "Not surprisingly, the state with the highest marriage rate is Nevada." _n
  378. in wh _dup(79) "-" _n(7) ;
  379. drop _all ;
  380. label drop _all ;
  381. macro define F6 "do %path`graphkit.tut;" ;
  382. set more 0 ; more ; set more 1 ;
  383.  
  384. di _n(4) in white
  385. "Demonstration ends" _n
  386. "------------------" _n ;
  387.  
  388.  
  389. di in green
  390. "That concludes our short demonstration, but there's much more.  We now return"
  391. _n
  392. "control to you.  Some suggestions:" _n ;
  393.  
  394. di in green
  395. "If you ..." _col(34) "Then we will show you ..." _n
  396. "    Press " in white "F5" in green _col(38) "a table of tutorial contents" _n
  397. "    Press " in white "F6" in green _col(38) "the next tutorial, "
  398. in white "graphkit.tut" _n ;
  399.  
  400. run %path`tobuy.tut ;
  401.